package structs;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import structs.form.ActionForm;
import structs.form.FullForm;

public class ActionServlet extends HttpServlet  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path=this.getPath(request.getServletPath());
		Map<String,XMLBean> map=(Map<String, XMLBean>) this.getServletContext().getAttribute("structs");
		XMLBean xml=map.get(path);
		String formClass=xml.getFormClass();//哪个类
		ActionForm form=FullForm.fullForm(formClass, request);
		request.setAttribute("form", form);
		String actionType=xml.getActionType();
		String url="";
		Action action=null;
		try{
			Class clazz=Class.forName(actionType);
			action=(Action) clazz.newInstance();
			url=action.excute(request, form, xml.getMapForword());
		}catch (Exception e) {
			// TODO: handle exception
		}
		RequestDispatcher dis=request.getRequestDispatcher(url);
		dis.forward(request, response);
	}
	public String getPath(String path){
		return path.split("\\.")[0];
	}
}
